package cn.hairui.fastms.base.controller;

import cn.hairui.fastms.base.cache.CacheManage;
import cn.hairui.fastms.base.config.WebLogInfo;
import cn.hairui.fastms.constant.FieldConstant;
import cn.hairui.fastms.constant.GlobalConstant;
import cn.hairui.fastms.core.bean.SBch;
import cn.hairui.fastms.core.bean.SResc;
import cn.hairui.fastms.core.bean.SUser;
import cn.hairui.fastms.core.service.SBchService;
import cn.hairui.fastms.core.service.SRescService;
import cn.hairui.fastms.util.LunarDateUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import cn.hutool.system.JavaInfo;
import cn.hutool.system.JvmInfo;
import cn.hutool.system.OsInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

@Controller
public class DefaultOperationController {
    @Autowired
    private SRescService sRescService;
    @Autowired
    private SBchService sBchService;

    private final static Logger logger = LoggerFactory.getLogger(DefaultOperationController.class);

    @WebLogInfo(description = "用户访问首页")
    @RequestMapping(value = "/")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        //获取用户登录信息
        SUser sUser = (SUser) request.getSession().getAttribute(GlobalConstant.GLOBAL_SESSION_NAME);
        if (sUser == null) {
            logger.info("USER           : {}", "");
            model.addAttribute("configMap", CacheManage.configMap);
            return "/login";
        } else {
            //获取顶级菜单
            List<SResc> menuList = new ArrayList<SResc>();
            String userAccount = sUser.getUserAccount();
            menuList = sRescService.selectMenuByAccount(userAccount, GlobalConstant.PARENT_RESC_ID_MAX);
            String userBch = sUser.getUserBch();
            if (userBch != null) {
                QueryWrapper<SBch> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq(FieldConstant.BCH_CDE[0], userBch);
                SBch sBch = sBchService.getOne(queryWrapper);
                model.addAttribute("orgName", sBch.getBchName());
            }
            model.addAttribute("menuList", menuList);
            model.addAttribute("configMap", CacheManage.configMap);
        }
        return "index";
    }

    @WebLogInfo(description = "用户访问登录页面")
    @GetMapping(value = "/login")
    public String login(HttpServletRequest request, Model model) {
        request.getSession().invalidate();
        //获取网站配置信息
        model.addAttribute("configMap", CacheManage.configMap);
        return "login";
    }

    @WebLogInfo(description = "用户退出")
    @RequestMapping(value = "/logout")
    public String logout(HttpServletRequest request, Model model) {
        request.getSession().invalidate();
        model.addAttribute("configMap", CacheManage.configMap);
        return "login";
    }

    @GetMapping(value = "/homepage")
    public String homepage(HttpServletRequest request, Model model) {
        String OPENDAY = DateUtil.today();
        model.addAttribute("OPENDAY", OPENDAY);
        //request.getSession().setAttribute(GlobalConstant.GLOBAL_OPENDAY, OPENDAY);
        DateTime date = DateUtil.date();
        Week week = DateUtil.dayOfWeekEnum(date);
        model.addAttribute("week", week.toChinese());

        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getDefault());
        LunarDateUtil lunar = new LunarDateUtil(cal);
        model.addAttribute("lunar", lunar.cyclical() + "年" + lunar.toString() + " " + lunar.animalsYear() + "年");

        model.addAttribute("jdkVersion",new JavaInfo().getVersion());
        model.addAttribute("jdkBit",new JvmInfo().getName());
        model.addAttribute("timeZoneId",TimeZone.getDefault().getID());
        model.addAttribute("osVersion",new OsInfo().getName());
        model.addAttribute("osArch",new OsInfo().getArch());

        return "homepage";
    }

}
